package com.thinkingstar.iads.projectcenter.jsoup.api;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import com.thinkingstar.iads.common.utils.FileCommonUtil;
import com.thinkingstar.iads.common.utils.InitSystemProperties;
import com.thinkingstar.iads.projectcenter.entity.PcProjectFile;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.thinkingstar.iads.common.filter.GlobalsFilter;
import com.thinkingstar.iads.common.filter.UserLoginFilter;
import com.thinkingstar.iads.common.utils.POIFileUtil;
import com.thinkingstar.iads.common.utils.PictureUtil;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectFile;

/**
 * 文件操作相关
 * @author Administrator
 *
 */
@IocBean
@At("/projectCenter/uploadFileApi")
@Filters({ @By(type = GlobalsFilter.class) , @By(type = UserLoginFilter.class)})
public class UploadFileApi {
	
	@Inject
	private DaoPcProjectFile daoPcProjectFile;
	
	private static Log log = Logs.get();
	
	@At("/uploadFile/?")
	@Ok("json")
	public Object uploadImage(String project_id , HttpServletRequest request){
		NutMap data_map = new NutMap();
		MultipartFile file=null;
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if(isMultipart){
        	ShiroHttpServletRequest shiroRequest = (ShiroHttpServletRequest) request;
        	CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        	MultipartHttpServletRequest multipartRequest = commonsMultipartResolver.resolveMultipart((HttpServletRequest) shiroRequest.getRequest()); 
        	file=multipartRequest.getFile("file");
        	InputStream fileStream;
    		try {
    			fileStream = file.getInputStream();
    			if(fileStream != null){
        			String upload_file_name = file.getOriginalFilename();//本地文件名称
        			String suffix_name = upload_file_name.substring(upload_file_name.indexOf(".")+1);//后缀名
        			//String new_file_name = upload_file_name;//新文件名
        			//String new_file_name = PictureUtil.createFileName(suffix_name);
        			String re_file_path = POIFileUtil.getMentionFileName(upload_file_name,suffix_name,project_id);//创建非结构化数据资料文件路径
        			String full_file_name = POIFileUtil.getAbFileNameByRepath(re_file_path); //绝对路径
        			Boolean is_success = PictureUtil.storeFile(fileStream, full_file_name);
        			if(is_success){
        				String file_id = daoPcProjectFile.saveFile(re_file_path,upload_file_name);
        				data_map.put("code", 0);
        				data_map.put("msg", "上传成功!");
						String serverIp= InitSystemProperties.get("preview.server.ip");
						String serverPort = InitSystemProperties.get("preview.server.port");
						String previewServer ="http://"+serverIp+":"+serverPort;
						PcProjectFile pcProjectFile = daoPcProjectFile.fetch(file_id);
						String relative_path = pcProjectFile.getRelativePaths();
						String ab_path = POIFileUtil.getFileURIPathByRepath(relative_path);
						String open_url = previewServer+ab_path;
						String url = previewServer+"/onlinePreview?url="+open_url;
						data_map.put("src", url); //图片src解析地址
						data_map.put("image_src", request.getContextPath()+"/datacenter/common/getFileById?file_id="+file_id); //图片src解析地址
        				data_map.put("original_file_name", upload_file_name); //文件原始名称
        				data_map.put("file_id", file_id); //文件相对地址
        				return data_map;
        			}
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
		}
		data_map.put("code", 1);
		data_map.put("msg", "文件上传失败!");
		return data_map;
	}
	

}
